package com.chhuang.ewcrm.frame;

import com.chhuang.ewcrm.cons.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * @author chhuang
 * @create 2020-10-16-16:47
 */
public class CameraFrame extends JFrame {
    private static final Logger logger = LoggerFactory.getLogger(CameraFrame.class);

    private static CameraFrame cameraFrame;

    private Box camerasPanel;
    private Box lprCamerasPanel;

    private static Dimension dim;
    private static int height;
    private static int width;

    /**{
     * 创建并显示GUI。出于线程安全的考虑，
     * 这个方法在事件调用线程中调用。
     */
    private CameraFrame(int cameraNum) {
        // 确保一个漂亮的外观风格
//        JFrame.setDefaultLookAndFeelDecorated(true);

        // 创建及设置窗口
        this.setTitle("监控摄像头");
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        /**获取屏幕的边界*/
        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
        /**获取底部任务栏高度*/
        int taskBarHeight = screenInsets.bottom;
        dim = Toolkit.getDefaultToolkit().getScreenSize();
        height = (int) dim.getHeight()-taskBarHeight-Constants.CONFIG_ROOT_HEAD_HEIGHT;
        width = height/cameraNum*11/9;
        logger.debug("窗口高宽："+height+", "+width);

//        this.setPreferredSize(new Dimension(width, height));
        this.setSize(width, height);
        this.setLocation((int)dim.getWidth()-width, Constants.CONFIG_ROOT_HEAD_HEIGHT);
        this.setAlwaysOnTop(true);

        // 指定 行数 和 列数 的网格布局, 并指定 水平 和 竖直 网格间隙
//        this.setLayout(new GridLayout(1, 2, 0, 0));
        this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));

        camerasPanel = new Box(BoxLayout.Y_AXIS);

        lprCamerasPanel = new Box(BoxLayout.Y_AXIS);

        this.add(camerasPanel);
        this.add(lprCamerasPanel);

        try {
            this.setIconImage(ImageIO.read(this.getClass().getResource("/img/guangfuu.png")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 显示窗口
//        this.pack();//自适应内部组件大小
        this.setVisible(true);
    }

    /**
     *
     * @return
     */
    public static synchronized CameraFrame getCameraFrame(int cameraNum){
        if(cameraFrame==null){
            cameraFrame = new CameraFrame(cameraNum);
        }else {
            width = width+height/cameraNum*11/9;
            cameraFrame.setSize(width, height);
            cameraFrame.setLocation((int)dim.getWidth()-width, Constants.CONFIG_ROOT_HEAD_HEIGHT);
            cameraFrame.repaint();
        }
        return cameraFrame;
    }

    /**
     *
     */
    public static synchronized void exit(){
        if(cameraFrame!=null){
            cameraFrame.dispose();
            cameraFrame = null;
        }
    }

    /**
     *
     * @param component
     */
    public void addCamerasPanel(Component component){
        JPanel jPanel = new JPanel(new GridLayout(1,1));
        jPanel.setBorder(BorderFactory.createLineBorder(new Color(153, 255, 102)));
        jPanel.add(component);
        camerasPanel.add(jPanel);
        camerasPanel.repaint();
    }

    /**
     *
     * @param component
     */
    public void addLprCamerasPanel(Component component){
        JPanel jPanel = new JPanel(new GridLayout(1,1));
        jPanel.setBorder(BorderFactory.createLineBorder(new Color(153, 255, 102)));
        jPanel.add(component);
        lprCamerasPanel.add(jPanel);
        lprCamerasPanel.repaint();
    }

}
